# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:mybro04.py
@Time:2022/3/1 10:15

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
from mybro03 import Mybro

def find_key(input_dict,value):
    return list({k for k,v in input_dict.items() if v==value})[0]

bro=Mybro()
bro.switchquery()
with open('zdmulu.csv','r',encoding='u8') as f:
    zdmulu=f.read()
    zddict=eval(zdmulu)
# zd="企业一套表统计调查制度(2022)"
zd=zddict['23']
print(zd)
zdcode=find_key(zddict,zddict['23'])
print(zdcode)
zqlist=bro.getyjnopts(zd)
print(zqlist)
with open('mulu.csv','w',encoding='u8') as f:
    #f.write(zd)
    f.write('')
i=1
with open('mulu.csv', 'a', encoding='u8') as f:
    f.write('{')
    for zq in zqlist:
        j=1
        bblist,bbhb=bro.getbblist1(zd,zq)
        #print(bblist)
        print(bbhb)
        for bb in bbhb:
            k=1
            bb,cxmllist=bro.getquery1(bb)
            # with open('mulu.csv', 'a', encoding='u8') as f:
            #     f.write('{')
            #print(f'制度：{zd},周期：{zq},报表：{bb},查询目录：{cxml}')
            for cxml in cxmllist:
                if j < 10:
                    if k < 10:
                        f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}0{j}':{bb},'{i}0{j}0{k}':'{cxml}'")
                        f.write(',\n')
                    else:
                        f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}0{j}':{bb},'{i}0{j}{k}':'{cxml}'")
                        f.write(',\n')
                else:
                    if k < 10:
                        f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}{j}':{bb},'{i}{j}0{k}':'{cxml}'")
                        f.write(',\n')
                    else:
                        f.write(f"'{zdcode}':'{zd}','{i}':'{zq}','{i}{j}':{bb},'{i}{j}{k}':'{cxml}'")
                        f.write(',\n')
                k+=1
            j+=1
        i+=1
    f.write('}')
bro.closewindow()